# -*- coding: utf-8 -*-
# @File    : db.py
# @Date    : 2021-11-25
# @Author  : Richard

import os
import time

import dmPython
from app.set_log import write_log


class DB(object):
    def __init__(self, sql, ip, user, port):
        self.sql = sql
        self.ip = ip
        self.user = user
        self.port = port

    def run_sql(self):
        try:
            # 这里是为了快速解决问题，建议使用特定方法使用字典方式获取密码同时加密
            if str(self.user).upper() == "WEBDOCUMENT":
                re_pwd = "!QAZxsw21qaz"
            elif str(self.user).upper() == "NOTICEV2":
                re_pwd = "!QAZxsw21qaz"
            elif str(self.user).upper() == "GOV_MEETING_ROOM_MANAGER":
                re_pwd = "GOV_MEETING_ROOM_MANAGER"
            elif str(self.user).upper() == "GOV_SYSTEM_MANAGEMENT":
                re_pwd = "GOV_SYSTEM_MANAGEMENT"
            elif str(self.user).upper() == "GOV_LEADERSHIP_AGENDA":
                re_pwd = "GOV_LEADERSHIP_AGENDA"
            elif str(self.user).upper() == "MP":
                re_pwd = "Dmdba@963"
            elif str(self.user).upper() == "PORTAL":
                re_pwd = "Dmdba@963"
            elif str(self.user).upper() == "OA9_5_0_GXBGT":
                re_pwd = "Dmdba@963"
            # 最高权限用户，有部分数据库节点的sysdba的密码有区别这里要做特殊处理
            elif str(self.user).upper() == "SYSDBA":
                ip_list = ["130.20.163.150", "130.20.164.17", "130.20.164.217", "130.20.163.199", "130.20.164.166",
                           "130.20.164.252", "130.20.164.113", "130.20.164.216", "130.20.164.5"]
                if self.ip in ip_list:
                    re_pwd = "Dmdba@123"
                else:
                    re_pwd = "Dmdba@963"
            # print("re_pwd:{0}".format(re_pwd))
            # print("user:{0}".format(self.user))
            # print("ip:{0}".format(self.ip))
            # print("ip:{0}".format(self.ip))
            # time.sleep(10000)
            conn = dmPython.connect(user=self.user, password=re_pwd, server=self.ip, port=self.port)
            cursor = conn.cursor()
            cursor.execute(self.sql)
            conn.commit()
            cursor.close()
            conn.close()
            return True
        except Exception as Err:
            print("Execute sql statement error is:{0}".format(Err))
            return False